﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuditModel;

namespace AuditWeb.Controllers
{
    [Authorize(Roles = "学科组组长")]
    public class ScorePercentageController : Controller
    {
        private AuditDBEntities db = new AuditDBEntities();
        private IAnnualEventBL iaebl = new AnnualEventBL();
        private IScoreBL isbl = new ScoreBL();
        //
        // GET: /ScorePercentage/

        [Authorize(Roles = "学科组组长")]
        public ViewResult Index(string id)
        {
            int subjectid=(int)HttpContext.Profile["SubjectId"];
            string annualeventid = id;
            if (string.IsNullOrEmpty(id))
                annualeventid = iaebl.GetActiveEvents().FirstOrDefault().ID;

            var scorepercentage = db.ScorePercentages.SingleOrDefault(sp => sp.AnnualEventID == annualeventid && sp.SubjectID == subjectid);//db.ScorePercentages.Include("AnnualEvent").Include("Subject");
            if (scorepercentage == null)
            {
                ScorePercentage sp = new ScorePercentage() { AnnualEventID = annualeventid, SubjectID = subjectid, PercentofInstruction =25m, PercentofDefense = 25m, PercentofMaterial = 25m, PercentofOthers = 25m };
                db.ScorePercentages.AddObject(sp);
                db.SaveChanges();
            }
            var scorepercentages = from sp in db.ScorePercentages where sp.AnnualEventID == annualeventid && sp.SubjectID == subjectid select sp;
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(annualeventid);
            ViewBag.AnnualEventId = annualeventid;
            ViewBag.SubjectName = db.Subjects.SingleOrDefault(s => s.Id == subjectid).Name;
             return View(scorepercentages.ToList());
        }

 
        //
        // GET: /ScorePercentage/Edit/5
        [Authorize(Roles = "学科组组长")]
        public ActionResult Edit(string id,int subjectid)
        {
            string annualeventid = id;
            ScorePercentage scorepercentage = db.ScorePercentages.Single(s => s.SubjectID == subjectid && s.AnnualEventID==annualeventid);
           
            //check if there are any scores. If there are some scores, the score percentage cann't be changed again.
            var scores=isbl.GetEventSubjectScores(annualeventid, subjectid);
            bool canedit = true;
            if (scores.Count() > 0)
                canedit = false;

            ViewBag.CanEdit = canedit;
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(annualeventid);
            ViewBag.AnnualEventId = annualeventid;
            ViewBag.SubjectName = db.Subjects.SingleOrDefault(s => s.Id == subjectid).Name;
            return View(scorepercentage);
        }

        //
        // POST: /ScorePercentage/Edit/5

        [HttpPost]
        [Authorize(Roles = "学科组组长")]
        public ActionResult Edit(ScorePercentage scorepercentage)
        {
            if ((scorepercentage.PercentofOthers + scorepercentage.PercentofDefense + scorepercentage.PercentofInstruction + scorepercentage.PercentofMaterial) > 100)
                ModelState.AddModelError("SubjectID", AuditResources.ScorePercentage.PercentageExceedingError);
            if (ModelState.IsValid)
            {
                db.ScorePercentages.Attach(scorepercentage);
                db.ObjectStateManager.ChangeObjectState(scorepercentage, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            //check if there are any scores. If there are some scores, the score percentage cann't be changed again.
            var scores = isbl.GetEventSubjectScores(scorepercentage.AnnualEventID, scorepercentage.SubjectID);
            bool canedit = true;
            if (scores.Count() > 0)
                canedit = false;

            ViewBag.CanEdit = canedit;
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(scorepercentage.AnnualEventID);
            ViewBag.AnnualEventId = scorepercentage.AnnualEventID;
            ViewBag.SubjectName = db.Subjects.SingleOrDefault(s => s.Id == scorepercentage.SubjectID).Name;

            return View(scorepercentage);
        }

        ////
        //// GET: /ScorePercentage/Details/5

        //public ViewResult Details(int id)
        //{
        //    ScorePercentage scorepercentage = db.ScorePercentages.Single(s => s.SubjectID == id);
        //    return View(scorepercentage);
        //}

        ////
        //// GET: /ScorePercentage/Create

        //public ActionResult Create()
        //{
        //    ViewBag.AnnualEventID = new SelectList(db.AnnualEvents, "ID", "Title");
        //    ViewBag.SubjectID = new SelectList(db.Subjects, "Id", "Name");
        //    return View();
        //}

        ////
        //// POST: /ScorePercentage/Create

        //[HttpPost]
        //public ActionResult Create(ScorePercentage scorepercentage)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.ScorePercentages.AddObject(scorepercentage);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    ViewBag.AnnualEventID = new SelectList(db.AnnualEvents, "ID", "Title", scorepercentage.AnnualEventID);
        //    ViewBag.SubjectID = new SelectList(db.Subjects, "Id", "Name", scorepercentage.SubjectID);
        //    return View(scorepercentage);
        //}
        

        ////
        //// GET: /ScorePercentage/Delete/5
 
        //public ActionResult Delete(int id)
        //{
        //    ScorePercentage scorepercentage = db.ScorePercentages.Single(s => s.SubjectID == id);
        //    return View(scorepercentage);
        //}

        ////
        //// POST: /ScorePercentage/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{            
        //    ScorePercentage scorepercentage = db.ScorePercentages.Single(s => s.SubjectID == id);
        //    db.ScorePercentages.DeleteObject(scorepercentage);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}